package cn.flying.cloud.system.role.service.impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.system.role.entity.SysRoleRes;
import cn.flying.cloud.system.role.mapper.SysRoleResMapper;
import cn.flying.cloud.system.role.service.SysRoleResService;

/**
 * <p>
 * 角色资源表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:08:05
 */
@Service
public class SysRoleResServiceImpl extends ServiceImpl<SysRoleResMapper, SysRoleRes> implements SysRoleResService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 分页查询角色资源表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysRoleRes> getPageList(PageRequest<SysRoleRes> request) {
        SysRoleRes param = request.getParam();
        LambdaQueryWrapper<SysRoleRes> wrapper = getLambdaQueryWrapper(param);
        Page<SysRoleRes> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysRoleRes> getLambdaQueryWrapper(SysRoleRes param) {
        LambdaQueryWrapper<SysRoleRes> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            wrapper.eq(SysRoleRes::getRoleId, param.getRoleId());
        }
        return wrapper;
    }

    /**
     * 列表查询角色资源表数据
     *
     * @param sysRoleRes
     * @return
     */
    @Override
    public List<SysRoleRes> getList(SysRoleRes sysRoleRes) {
        LambdaQueryWrapper<SysRoleRes> wrapper = getLambdaQueryWrapper(sysRoleRes);
        List<SysRoleRes> list = this.list(wrapper);
        return list;
    }

    /**
     * 查询角色资源表详情数据
     *
     * @param sysRoleRes
     * @return
     */
    @Override
    public SysRoleRes getOneObj(SysRoleRes sysRoleRes) {
        LambdaQueryWrapper<SysRoleRes> wrapper = new LambdaQueryWrapper<>();
        return this.getOne(wrapper);
    }


    /**
     * 新增角色资源表数据
     *
     * @param sysRoleRes
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysRoleRes sysRoleRes) {
        return this.save(sysRoleRes);
    }

    /**
     * 更新角色资源表数据
     *
     * @param sysRoleRes
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysRoleRes sysRoleRes) {
        return this.saveOrUpdate(sysRoleRes);
    }

    /**
     * 删除角色资源表数据
     *
     * @param sysRoleRes
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysRoleRes sysRoleRes) {
        LambdaQueryWrapper<SysRoleRes> wrapper = new LambdaQueryWrapper<>();
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }

    /**
     * 批量新增角色资源表数据
     *
     * @param sysRoleRes
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRoleRes(List<SysRoleRes> sysRoleRes) {
        LambdaQueryWrapper<SysRoleRes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleRes::getRoleId, sysRoleRes.get(0).getRoleId());
        this.remove(wrapper);
        this.saveBatch(sysRoleRes);
    }
}
